package com.earl.eatbao.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.earl.eatbao.commons.vo.ResultMessage;
import com.earl.eatbao.domain.eatbao.Shop;
import com.earl.eatbao.service.ShopService;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @author Administrator
 * @version 1.0
 * @created 22-十一月-2016 22:34:46
 */
@RestController
//@Controller
@RequestMapping(value="/shop")
public class ShopController extends BaseController{

	private final Logger logger = LoggerFactory.getLogger(ShopController.class);
	
    @Autowired
    private ShopService shopService;

	/**
	 * 查询商家所有商品
	 * @param businessId
	 * @return
	 */
	@ApiOperation(value = "查询商家所有商品", notes = "查询商家所有商品", httpMethod = "GET", response = String.class)
	@RequestMapping(value="/getBusinessShop",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findBusinessShop(
			@ApiParam(name = "businessId", required = true, value = "商家Id")
			@RequestParam(name = "businessId", required = true)long businessId) {
		logger.info("<====== 进入shopOnline:方法,{[businessId] :" + businessId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("shopList", shopService.findBusinessShop(businessId));
		result.setMessageCode(200l);
		if (null != result.getResultParm().get("shopList")) {
			result.setServiceResult(true);
			result.setResultInfo("请求操作成功");
		} else {
			result.setServiceResult(false);
		    result.setResultInfo("请求操作失败");
		}

		logger.info("==== 退出shopOnline方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
    
	/**
	 * 商店营业，上线
	 * 
	 * @param userId 商家账号id
	 */
	@ApiOperation(value = "商店营业，上线", notes = "商店营业，上线", httpMethod = "POST", response = String.class)
    @RequestMapping(value = "/online/{userId}",produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> shopOnline(
			@ApiParam(name = "userId", value = "商家账号ID")
			@PathVariable
			Long userId){
		logger.info("<====== 进入shopOnline:方法,{[userId] : + " + userId + " } ====");

		ResultMessage resultMessage = new ResultMessage();
		shopService.shopOnline(userId);
        resultMessage.setResultInfo("请求操作成功");
        resultMessage.setServiceResult(true);

		logger.info("==== 退出shopOnline方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(resultMessage,HttpStatus.OK);
	}

	/**
	 * 查询所有上线的商店
	 * 
	 * @param userId 商家账号id
	 */
	@ApiOperation(value = "商店营业，上线", notes = "商店营业，上线", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/online/all",produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> findOnlineShop(){
		
		ResultMessage resultMessage = new ResultMessage();
		
		resultMessage.getResultParm().put("shopList", shopService.findOnlineShop());
		resultMessage.setResultInfo("请求操作成功");
		resultMessage.setServiceResult(true);
		
		return new ResponseEntity<ResultMessage>(resultMessage,HttpStatus.OK);
	}

	/**
	 * 商店下线，不营业
	 * 
	 * @param userId 商家账号id
	 */
	@ApiOperation(value = "商店下线，不营业", notes = "商店下线，不营业", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/offline/{userId}",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> shopOffLine(
			@ApiParam(name = "userId", required = true, value = "商家账号id")
			@PathVariable
			Long userId){
		logger.info("<====== 进入shopOffLine:方法,{[userId] : " + userId + " } ====");
		// desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        result.setServiceResult(true);
        shopService.shopOffline(userId);
        result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出deleteUserAllShop方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 删除指定商店
	 * @param shopId 商店ID
	 * @return
	 */
	@ApiOperation(value = "删除指定商店", notes = "删除指定商店", httpMethod = "DELETE", response = String.class)
	@RequestMapping(value="/delete/{shopId}",  method = RequestMethod.DELETE)
	public ResponseEntity<ResultMessage> deleteShop(
			@ApiParam(name="shopId", value="商店ID", required= true)
			@PathVariable
			Long shopId) {
		logger.info("<====== 进入deleteShop:方法,{[shopId] : " + shopId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(true);
		shopService.delete(shopId);
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出deleteShop方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 删除指定商家的使用商店
	 * @param userId 商家ID
	 * @return
	 */
	@ApiOperation(value = "删除指定商家的使用商店", notes = "删除指定商家的使用商店", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/deleteUserAllShop",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> deleteUserAllShop(
			@ApiParam(name="userId", value="商家ID", required= true)
			@RequestParam(name = "userId", required = true) Long userId) {
		logger.info("<====== 进入deleteUserAllShop:方法,{[userId] : " + userId + " } ====");
		// desc: 封装操作结果
		ResultMessage result = new ResultMessage();
		result.setServiceResult(true);
		shopService.delete(userId);
		result.setMessageCode(200l);
		result.setResultInfo("请求操作成功");

		logger.info("==== 退出deleteUserAllShop方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

    /**
     * 添加商店
     * @param shop
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startingPrice",value = "起送价", required = true, dataType = "Int", paramType = "Query"),
            @ApiImplicitParam(name = "distributionFee",value = "配送费", required = true, dataType = "Int", paramType = "Query"),
            @ApiImplicitParam(name = "latitude",value = "纬度", required = true, dataType = "double", paramType = "Query"),
            @ApiImplicitParam(name = "longitude",value = "经度", required = true, dataType = "double", paramType = "Query"),
            @ApiImplicitParam(name = "shopName",value = "商店名称", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "license",value = "营业许可", required = true, dataType = "Int", paramType = "Query"),
            @ApiImplicitParam(name = "stopTime", value = "关店时间",required = true, dataType = "Int", paramType = "Query"),
            @ApiImplicitParam(name = "businessId", value = "商家Id",required = true, dataType = "long", paramType = "Query"),
            @ApiImplicitParam(name = "openTime", value = "开店时间",required = true, dataType = "Int", paramType = "Query")
    })
	@ApiOperation(value = "添加商店", notes = "添加商店", httpMethod = "POST", response = String.class)
	@RequestMapping(value="/add",  method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> addShop(
			Shop shop,
			@ApiParam(name = "file", required = true, value = "上传商店图片")
			@RequestParam(name = "file", required = true) MultipartFile file) {
        logger.info("<====== 进入addShop:方法,{[shop, file]:" + shop.toString() + "} ====");
        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();

			result.getResultParm().put("shop", shopService.addShop(shop, file));
			if (null != result.getResultParm().get("shop")) {
				result.setServiceResult(true);
				result.setResultInfo("请求操作成功");
			} else {
				result.setServiceResult(false);
				result.setResultInfo("请求操作失败");
			}
        result.setMessageCode(200l);
        logger.info("==== 退出addShop方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }

    /**
     * 获取所有商店
     * @return
     */
    @ApiOperation(value = "获得所有商店信息", notes = "获得所有商店信息", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/all",  method = RequestMethod.GET)
    public ResponseEntity<ResultMessage> findAll(){
        logger.info("<====== 进入findAll:方法,{[] : } ====");
        ResultMessage result = new ResultMessage();
    	result.setMessageCode(200l);
        result.getResultParm().put("shopList", shopService.findAll());
        if (null != result.getResultParm().get("shopList")) {
            result.setResultInfo("查询成功");
        } else {
            result.setResultInfo("查询不成功");
        }

    	logger.info("==== 退出findAll方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
    	return new ResponseEntity<ResultMessage>(result,HttpStatus.OK);
    }

	/**
	 * 商店图片上传
	 * @param file
	 * @param shopId
	 * @return
	 */
    @ApiOperation(value = "上传商店图片", notes = "上传商店图片", httpMethod = "POST", response = String.class)
    @RequestMapping(value="/uploadImage",  method = RequestMethod.POST)
    public ResponseEntity<ResultMessage> upLoadShopImage(
    		@ApiParam(name = "file", value = "上传文件")
			@RequestParam("file") MultipartFile file,
			@ApiParam(name = "shopId", required = true, value = "商店ID")
			@RequestParam(name = "shopId", required = true) Long shopId) {
		logger.info("<====== 进入upLoadShopImage:方法,{[file, shopId] :" + shopId + " } ====");
		// desc: 封装操作结果
    	ResultMessage result = new ResultMessage();
    	result.setServiceResult(shopService.upLoadShopImage(file, shopId));
    	result.setMessageCode(200l);
    	if (result.getServiceResult()) {
			result.setResultInfo("请求操作成功");
		} else {
			result.setResultInfo("请求操作失败");
		}

    	logger.info("==== 退出upLoadShopImage方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
    	return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}


    /**
     * 获取指点商店
     * @return
     */
    @ApiOperation(value = "获取指定商店", notes = "获取指定商店", httpMethod = "GET", response = String.class)
    @RequestMapping(value="/getShop/{shopId}",  method = RequestMethod.GET)
	public ResponseEntity<ResultMessage> getShop(
	        @ApiParam(name = "shopId", required = true, value = "商店ID")
	        @PathVariable long shopId) {
        logger.info("<====== 进入getShop:方法,{[shopId] :" + shopId + " } ====");
        // desc: 封装操作结果
        ResultMessage result = new ResultMessage();
        result.getResultParm().put("shop",shopService.findOne(shopId));
        result.setMessageCode(200l);
        if (null != result.getResultParm().get("shop")) {
            result.setServiceResult(true);
            result.setResultInfo("请求操作成功");
        } else {
            result.setServiceResult(false);
            result.setResultInfo("请求操作失败");
        }

        logger.info("==== 退出getShop方法 返回值类型:{ ResponseEntity<ResultMessage>: } =======>");
        return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
    }
    
}